﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mscook.core.services;
using mscook.core;
namespace mscook.mvc.Areas.weblogin.Controllers
{

    public class PlatImageController : BaseController
    {
        //
        // GET: /weblogin/Image/

        protected IImageSettingFactory iImageSettingFactory = UnityHelper.UnityToT<IImageSettingFactory>();
        private ILoginUserService iLoginUserService;
        public PlatImageController(ILoginUserService iLoginUserService):base(iLoginUserService)
        {
            this.iLoginUserService = iLoginUserService;
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(string deleteId)
        {
            mscook.core.services.ImageSettingFactory.ImageSettingSection selecSection = iImageSettingFactory.SelectSection(deleteId);
            iImageSettingFactory.DeleteSection(selecSection);

            return RedirectToIndex();
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(string uid)
        {
            mscook.core.services.ImageSettingFactory.ImageSettingSection section = iImageSettingFactory.SelectSection(uid);
            InitNavBar();
            return View(section);
        }

        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult Edit(mscook.core.services.ImageSettingFactory.ImageSettingSection section)
        {
            mscook.core.services.ImageSettingFactory.ImageSettingSection selecSection = iImageSettingFactory.SelectSection(section.alias);
            section.ImageSettings = selecSection.ImageSettings;
            iImageSettingFactory.UpdateSection(section);
            InitNavBar();
            return RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Add()
        {
            //ViewBag.SelectList = iImageSettingFactory.SelectUnSetSections();
            SelectList selectList=new SelectList(iImageSettingFactory.SelectUnSetSections());
            ViewData.Add("alias",selectList);
            InitNavBar();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult Add(mscook.core.services.ImageSettingFactory.ImageSettingSection section)
        {
            if (!string.IsNullOrEmpty(section.name) && !string.IsNullOrEmpty(section.alias))
            {

                 iImageSettingFactory.SaveSection(section);

            }
            return RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public string DeleteImageSetting(string uid, string sectionId)
        {
            if (!string.IsNullOrEmpty(uid) && !string.IsNullOrEmpty(sectionId))
            {
                mscook.core.services.ImageSettingFactory.ImageSettingSection section = iImageSettingFactory.SelectSection(sectionId);


                mscook.core.services.ImageSettingFactory.ImageSetting imageSetting = section.ImageSettings.Where(o => o.folder == uid).SingleOrDefault();

                section.ImageSettings.Remove(imageSetting);
                iImageSettingFactory.UpdateSection(section);
                return true.ToString();

            }
            return false.ToString();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public string EditImageSetting(string uid, string folder, string width, string height, string sectionId)
        {

            int parseWidth = 0;
            int parseHeight = 0;
            if (!string.IsNullOrEmpty(folder) && Int32.TryParse(width, out parseWidth) && Int32.TryParse(height, out parseHeight))
            {
                mscook.core.services.ImageSettingFactory.ImageSettingSection section = iImageSettingFactory.SelectSection(sectionId);


                mscook.core.services.ImageSettingFactory.ImageSetting imageSetting = section.ImageSettings.Where(o => o.folder == folder).SingleOrDefault();
                //.Add(new mscook.core.services.ImageSettingFactory.ImageSetting { folder = folder, width = parseWidth, height = parseHeight });
                if (imageSetting == null)
                {
                    imageSetting = new ImageSettingFactory.ImageSetting() { folder = folder, width = parseWidth, height = parseHeight };
                    section.ImageSettings.Add(imageSetting);
                }
                else
                {
                    imageSetting.height = parseHeight;
                    imageSetting.width = parseWidth;
                    imageSetting.folder = folder;
                }
                iImageSettingFactory.UpdateSection(section);
                //mscook.core.services.ImageSettingFactory.ImageSetting image = new ImageSettingFactory.ImageSetting();



                return true.ToString();
            }
            return false.ToString();
            //return View();
        }

        public ActionResult Index()
        {

            IList<mscook.core.services.ImageSettingFactory.ImageSettingSection> sectionList = iImageSettingFactory.SelectSections();



            InitNavBar();
            return View(sectionList);
        }

    }
}
